﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BUS;
using DTO;
using System.Collections.Generic;
using TuyenDungViecLam.CommonResource;
namespace TuyenDungViecLam.User_control_VN
{
    public partial class Insert_Job : System.Web.UI.UserControl
    {
        protected void chitietcongviec_Click(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NhaTuyenDung")
            {
                string url = "SuaTinTuyenDung.aspx?Mct=" + Request.QueryString["Mct"] + "&Mcv=" + macongviec.Text + "&Edit=0";
                Response.Redirect(url);
            }
            if (Session["Role"].ToString() == "Administrator")
            {
                string url = "SuaTin.aspx?Mct=" + ViewState["Mct"] + "&Mcv=" + macongviec.Text + "&Edit=0";
                Response.Redirect(url);
            }
        }


        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "Administrator")
            {
                nhatuyendung.Visible = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dlnhatuyendung.DataSource = NhaTuyenDung_BUS.DanhSachNhaTuyenDung();
                dlnhatuyendung.DataTextField = "Ten";
                dlnhatuyendung.DataValueField = "MaNhaTuyenDung";
                dlnhatuyendung.DataBind();

                dlcapbac.DataSource = ChiTietCongViec_BUS.DanhSachCacCapBac();
                dlcapbac.DataTextField = "Ten";
                dlcapbac.DataValueField = "MaCapBac";
                dlcapbac.DataBind();

                dlloaihinhcongviec.DataSource = ChiTietCongViec_BUS.DanhSachCacLoaiHinhCongViec();
                dlloaihinhcongviec.DataTextField = "TenLoaiHinh";
                dlloaihinhcongviec.DataValueField = "MaLoaiHinhCongViec";
                dlloaihinhcongviec.DataBind();

                dlnoilamviec.DataSource = ChiTietCongViec_BUS.DanhSachNoiLamViec();
                dlnoilamviec.DataTextField = "Ten";
                dlnoilamviec.DataValueField = "MaTinhThanh";
                dlnoilamviec.DataBind();

                rpnganhnghe.DataSource = NganhNghe_BUS.DanhSachCacNganhNghe();
                rpnganhnghe.DataBind();
                tbngayhethandang.Text = DateTime.Now.ToString();
            }
        }

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            if (tbchucdanh.Text != "")
            {
                ChiTietCongViec_DTO congviec = new ChiTietCongViec_DTO();
                congviec.ChucDanh = tbchucdanh.Text;
                congviec.MoTaCongViec = tbmotacongviec.Text;
                congviec.YeuCauCongViec = tbyeucaucongviec.Text;
                congviec.NgonNgu = tbngonngu.Text;

                congviec.MaCapBac = int.Parse(dlcapbac.SelectedItem.Value);
                congviec.Mucluong = tbmucluong.Text;
                if (tbluong.Text != "")
                    congviec.Luong = int.Parse(tbluong.Text);
                congviec.NamKinhNghiem = tbsonamkinhnghiem.Text;
                if (tbsonkn.Text != "")
                    congviec.SoNamKN = int.Parse(tbsonkn.Text);
                congviec.MaLoaiHinhCongViec = int.Parse(dlloaihinhcongviec.SelectedItem.Value);
                congviec.NgayDang = DateTime.Today;
                congviec.NgayHetHanDang = DateTime.Parse(tbngayhethandang.Text);
                if (congviec.NgayDang < congviec.NgayHetHanDang)
                {
                    congviec.MaNoiLamViec = int.Parse(dlnoilamviec.SelectedItem.Value);
                    int id = 0;
                    if (Session["Role"].ToString() == "NhaTuyenDung")
                    {
                        id = NhaTuyenDung_BUS.ThemTinTuyenDung(congviec, Request.QueryString["Mct"]);
                    }
                    if (Session["Role"].ToString() == "Administrator")
                    {
                        ViewState["Mct"] = dlnhatuyendung.SelectedItem.Value;
                        id = NhaTuyenDung_BUS.ThemTinTuyenDung(congviec, dlnhatuyendung.SelectedItem.Value);
                    }

                    macongviec.Text = id.ToString();
                    for (int i = 0; i < rpnganhnghe.Items.Count; i++)
                    {
                        CheckBox cbchon = (CheckBox)rpnganhnghe.Items[i].FindControl("cbchon");
                        Label lbmanganhnghe = (Label)rpnganhnghe.Items[i].FindControl("lbMng");
                        if (id != -1 && cbchon.Checked == true)
                        {
                            CONGVIEC_NGANHNGHE congviec_nganhnghe = new CONGVIEC_NGANHNGHE();
                            congviec_nganhnghe.MaCongViec = id;
                            congviec_nganhnghe.MaNganhNghe = int.Parse(lbmanganhnghe.Text);
                            NganhNghe_BUS.ThemCongViec_NganhNghe(congviec_nganhnghe);
                        }
                    }
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                }
                else
                {
                    Alert.Show("Vui lòng chọn ngày hết hạn đăng lớn hơn ngày hôm nay !");
                }
            }
            else
            {
                Alert.Show("Vui lòng điền chức danh công việc, và một số tin công việc nếu cần thiết trước khi đăng tin !");
            }
        }


        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["Role"].ToString() == "NhaTuyenDung")
            {
                string url = "DangTinTuyenDung.aspx?Mct=" + Request.QueryString["Mct"];
                Response.Redirect(url);
            }
            if (Session["Role"].ToString() == "Administrator")
            {
                string url = "CacTinDang.aspx";
                Response.Redirect(url);
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["Role"].ToString() == "NhaTuyenDung")
            {
                string url = "DangTinTuyenDung.aspx?Mct=" + Request.QueryString["Mct"];
                Response.Redirect(url);
            }
            if (Session["Role"].ToString() == "Administrator")
            {
                string url = "CacTinDang.aspx";
                Response.Redirect(url);
            }
        }
    }
}